<?php
fily_XMLConverter::registerNamespace('popuppanel', fily_PopupPanel);
class fily_PopupPanel extends fily_Panel {
    /**
     * trigger which opens the popup
     * @var array
     */
    protected $openTriggers = array();

    /**
     * trigger which closes the popup
     * @var array
     */
    protected $closeTriggers = array();

    /**
     * trigger which closes the popup always
     * @var array
     */
    protected $cancelTriggers = array();


    public function __construct() {
        parent::__construct();
        $this->getStyle()->set('display', 'none');
    }

    /**
     * prepares the code for the html output
     */
    public function prepare() {
        parent::prepare();

        if(count($this->openTriggers)>0||count($this->closeTriggers)||count($this->cancelTriggers)){
            $code = sprintf("var fily_temp_init_%s_PopupPanel = firelynx.PopupPanel('%s');",$this->getId(),$this->getId() );
            fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($this->getId());
        }


        foreach ($this->openTriggers as $trigger) {
            $code = sprintf("fily_temp_init_%s_PopupPanel.addOpenTrigger('%s', '%s', %s);",$this->getId(),$trigger['control']->getId(), $trigger['event'], $trigger['executionTime'] );
             fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($trigger['control']->getId());
        }

        foreach ($this->closeTriggers as $trigger) {
            $code = sprintf("fily_temp_init_%s_PopupPanel.addCloseTrigger('%s', '%s', %s);",$this->getId(),$trigger['control']->getId(), $trigger['event'], $trigger['executionTime'] );
             fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($trigger['control']->getId());
        }

        foreach ($this->cancelTriggers as $trigger) {
            $code = sprintf("fily_temp_init_%s_PopupPanel.addCancelTrigger('%s', '%s', %s);",$this->getId(),$trigger['control']->getId(), $trigger['event'], $trigger['executionTime'] );
            fily_ClientCodeManager::addCode($code);
            fily_ClientCodeManager::useId($trigger['control']->getId());
        }

    }
    /**
     * Add a trigger that opens the popup
     * @param fily_Control $control the control that triggers the event
     * @param string $event javascript event (onclick, onchange)
     */
    public function addOpenTrigger(fily_Control $control, $event, $executionTime = 900 ) {
        $this->openTriggers[] = array('control' => $control,'event' =>  $event,'executionTime' => $executionTime);
    }


    /**
     * Add a trigger that closes the popup
     * @param fily_Control $control the control that triggers the event
     * @param string $event javascript event (onclick, onchange)
     */
    public function addCloseTrigger(fily_Control $control, $event,  $executionTime = 900) {
        $this->closeTriggers[]  = array('control' => $control,'event' =>  $event,'executionTime' => $executionTime);
    }


    /**
     * Add a trigger that closes the popup always
     * @param fily_Control $control the control that triggers the event
     * @param string $event javascript event (onclick, onchange)
     */
    public function addCancelTrigger(fily_Control $control, $event,  $executionTime = 900) {
        $this->cancelTriggers[]  = array('control' => $control,'event' =>  $event,'executionTime' => $executionTime);
    }

    public function XMLCallFunction($name, array $parameters,$innerValue,  fily_ControlToHtml $innerControl = null) {
        switch ($name) {
            case 'opentrigger':
                $this->lateBindingObjects['opentrigger'][] = $parameters;
                break;
            case 'closetrigger':
                $this->lateBindingObjects['closetrigger'][] = $parameters;
                break;
            case 'canceltrigger':
                $this->lateBindingObjects['canceltrigger'][] = $parameters;
                break;
            default :
                // function not handled
                parent::XMLCallFunction($name, $parameters);
                break;
        }

    }
    public function XMLLateBinding(fily_ControlToHtml $baseControl) {
        $deleteArray = array();
        foreach($this->lateBindingObjects as $key => $value) {
            switch ($key) {
                case 'opentrigger':
                    foreach ($this->lateBindingObjects[$key] as $parameters) {
                        $control = fily_XMLConverter::findControl($parameters['control'], $this, $baseControl);

                        if($control!=null) {
                            if(!isset($parameters['executionzone'])) {
                                $this->addOpenTrigger($control, $parameters['event']);
                            }
                            else {
                                $this->addOpenTrigger($control, $parameters['event'],$parameters['executionzone']);
                            }
                        }
                    }
                    unset($this->lateBindingObjects[$key]);
                    break;
                case 'closetrigger':
                    foreach ($this->lateBindingObjects[$key] as $parameters) {
                      $control = fily_XMLConverter::findControl($parameters['control'], $this, $baseControl);

                        if($control!=null) {
                            if(!isset($parameters['executionzone'])) {
                                $this->addCloseTrigger($control, $parameters['event']);
                            }
                            else {
                                $this->addCloseTrigger($control, $parameters['event'],$parameters['executionzone']);
                            }
                        }
                    }
                     unset($this->lateBindingObjects[$key]);
                    break;
                case 'canceltrigger':
                    foreach ($this->lateBindingObjects[$key] as $parameters) {
                        $control = fily_XMLConverter::findControl($parameters['control'], $this, $baseControl);

                        if($control!=null) {
                            if(!isset($parameters['executionzone'])) {
                                $this->addCancelTrigger($control, $parameters['event']);
                            }
                            else {
                                $this->addCancelTrigger($control, $parameters['event'],$parameters['executionzone']);
                            }
                        }
                    }
                   unset($this->lateBindingObjects[$key]);
                    break;
            }
        }

        parent::XMLLateBinding($baseControl);
    }



}

?>
